package color.koitq.picker.adapter;

import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import color.koitq.picker.R;
import color.koitq.picker.entity.HomeModel;
import color.koitq.picker.entity.PaletteModel;

public class HomeAdapter extends BaseMultiItemQuickAdapter<HomeModel, BaseViewHolder> {
    private HomeSubAdapter mAdapter1, mAdapter2;
    private HomeItemClickListener mHomeItemClickListener;

    public HomeAdapter setHomeItemClickListener(HomeItemClickListener homeItemClickListener) {
        mHomeItemClickListener = homeItemClickListener;
        return this;
    }

    public HomeAdapter() {
        super();
        addItemType(1, R.layout.item_home_one);
        addItemType(2, R.layout.item_home_two);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder baseViewHolder, HomeModel lists) {
        List<PaletteModel> list = lists.mModels;
        if (list.size() > 1) {
            baseViewHolder.setText(R.id.title1, lists.mModels.get(0).getTitle());
            baseViewHolder.setText(R.id.title2, lists.mModels.get(1).getTitle());

            RecyclerView list1 = baseViewHolder.getView(R.id.list1);
            list1.setLayoutManager(new LinearLayoutManager(getContext()));
            mAdapter1 = new HomeSubAdapter();
            list1.setAdapter(mAdapter1);
            String colors0 = lists.mModels.get(0).getColors();
            if (!TextUtils.isEmpty(colors0) && colors0.contains(",")) {
                String[] split = colors0.split(",");
                List<String> colorStr0 = new ArrayList<>(Arrays.asList(split));
                mAdapter1.setNewInstance(colorStr0);
            }


            RecyclerView list2 = baseViewHolder.getView(R.id.list2);
            list2.setLayoutManager(new LinearLayoutManager(getContext()));
            mAdapter2 = new HomeSubAdapter();
            list2.setAdapter(mAdapter2);
            String colors1 = lists.mModels.get(1).getColors();
            if (!TextUtils.isEmpty(colors1) && colors1.contains(",")) {
                String[] split = colors1.split(",");
                List<String> colorStr1 = new ArrayList<>(Arrays.asList(split));
                mAdapter2.setNewInstance(colorStr1);
            }
        }
        baseViewHolder.getView(R.id.view1).setOnClickListener(v -> {
            if (mHomeItemClickListener != null) {
                mHomeItemClickListener.click(lists.mModels.get(0).getPaletteId());
            }
        });
        baseViewHolder.getView(R.id.view2).setOnClickListener(v -> {
            if (mHomeItemClickListener != null) {
                mHomeItemClickListener.click(lists.mModels.get(1).getPaletteId());
            }
        });

    }

    public interface HomeItemClickListener {
        void click(int id);
    }

}
